package so.ifl.servcie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.StringHelper;
import so.ifl.dao.AccountInfoDao;
import so.ifl.dao.SysDao;
import so.ifl.servcie.AccountService;
import so.ifl.vo.AccountVo;

/**
 *create by aaron.lu
 *create at 2012-1-7 下午3:24:56
 *<mailto: atyuan.lu@gmail.com>
 **/
@Service("accountService")
public class AccountServiceImpl implements AccountService{
	
	@Autowired
	private AccountInfoDao accountInfoDao;
	
	@Autowired
	private SysDao sysDao;
	
	Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 登录查询
	 * create by aaron.lu
	 * create at 2012-4-27 上午11:20:36
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public List<AccountVo> queryLoginAccount(Map<String, Object> params){
		List<AccountVo> list = accountInfoDao.queryAccountWithParamAndPagination(params, new RowBounds(0, 1));
		if (null != list & list.size() > 0) {
			AccountVo accountVo = list.get(0);
			params.put("accout_id_", accountVo.getAccoutId());
			List<Map<String, Object>> arrList = accountInfoDao.queryARR(params);
			if (null != arrList & arrList.size() > 0) {
				StringBuffer hasRole = new StringBuffer("");
				for (int i = 0; i < arrList.size(); i++) {
					Map<String, Object> map = arrList.get(i);
					Long role_id_ = (Long)map.get("role_id_");
					
					if (null != role_id_) {
						if (i != (arrList.size() - 1)) {
							hasRole.append(role_id_ + ",");
						}else{
							hasRole.append(role_id_);
						}
					}
				}
				accountVo.setHasRoles(hasRole.toString());
			}
		}
		return list;
		
	}
	
	
	/**
	 * 查询帐号信息
	 * create by aaron.lu
	 * create at 2012-1-8 下午1:36:29
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @param limmit
	 * @param offset
	 * @return
	 *
	 */
	public List<AccountVo> queryAccountWithParamAndPagination(Map<String, Object> params, Integer offset, Integer limit){
		List<AccountVo> list = accountInfoDao.queryAccountWithParamAndPagination(params, new RowBounds(offset, limit));
		return list;
	}
	
	/**
	 * 统计帐号信息
	 * create by aaron.lu
	 * create at 2012-1-8 下午1:39:21
	 * <mailto: atyuan.lu@gmail.com>
	 * @param map
	 * @return
	 *
	 */
	public int countAccountWithParam(Map<String, Object> params){
		return accountInfoDao.countAccountWithParam(params);
	}
	
	/**
	 * 新增帐号信息
	 * create by aaron.lu
	 * create at 2012-1-8 下午1:42:44
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 *
	 */
	public void insertAccount(Map<String, Object> params){
		String account_login_name_ = sysDao.getSeq(Constant.JXT_ACCOUNT_SEQ);
		params.put("account_login_name_", account_login_name_);//登录帐号自动生成
		accountInfoDao.insertAccount(params);
	}
	
	/**
	 * 更新帐号信息
	 * create by aaron.lu
	 * create at 2012-1-8 下午1:44:56
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 *
	 */
	public void updateAccount(Map<String, Object> params){
		accountInfoDao.updateAccount(params);
	}
	
	/**
	 * 删除帐号信息
	 * create by aaron.lu
	 * create at 2012-1-8 下午1:46:01
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 *
	 */
	public void deleteAccount(Integer accountid){
		accountInfoDao.deleteAccount(accountid);
	}
	
	/**
	 * 初始化应用
	 * create by aaron.lu
	 * create at 2012-1-10 下午4:13:02
	 * <mailto: atyuan.lu@gmail.com>
	 * @param initWord
	 * @param initPwd
	 *
	 */
	public void firstInitApp(String initWord, String initPwd){
		
		//do nothing
	}
	
	/**
	 * 保存更新过的用户角色
	 * create by aaron.lu
	 * create at 2012-1-18 上午12:43:35
	 * <mailto: atyuan.lu@gmail.com>
	 * @param roleId
	 * @param accountId
	 *
	 */
	public void editRoleOfAccount(String roleId, String accountId){
		if (StringHelper.isNotEmpty(roleId) && StringHelper.isNotEmpty(accountId)) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("accout_id_", accountId);
			//每个账号只能拥有一个角色，仅以账号查出现有的角色。
			
			List<Map<String, Object>> list = accountInfoDao.queryARR(params);
			
			if (null != list && list.size() > 0) {
				Map<String, Object> exist_mm = list.get(0);
				exist_mm.put("role_id_", roleId);
				accountInfoDao.updateRoleMmAccount(exist_mm);
			}else{
				//未拥有过角色
				params.put("role_id_", roleId);
				accountInfoDao.insertRoleMmAccount(params);
			}
		}
	}

	
	
}
